#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int hash[26] = { 0 };
        int cmp[26] = { 0 };
        for (auto e : p)
        {
            cmp[e - 'a']++;
        }
        for (int left = 0, right = 0, count = 0; right < s.size(); ++right)
        {
            if (cmp[s[right] - 'a'] > hash[s[right] - 'a']++) ++count;
            while (right - left + 1 > p.size())
            {
                if (cmp[s[left] - 'a'] >= hash[s[left++] - 'a']--) --count;
            }
            if (count == p.size())
                ret.push_back(left);
        }
        return ret;
    }
};